//index.js
//获取应用实例
const app = getApp()

Page({
    data: {
        motto: 'Hello World',
        userInfo: {},
        hasUserInfo: false,
        canIUse: wx.canIUse('button.open-type.getUserInfo'),

        openId: null,
        studentInfo: null,
        hasReg: false,

        schoolRange: ['请选择学校'],
        gradeRange: ['请选择年级'],
        classRange: ['请选择班级'],

        questionsNumber: 0,
        pushquestionsNumber: 0,

        showPopup: 0,
    },
    onShow: function() {
        // show pop
        this.setData({
            showPopup: app.globalData.showPopup,
        })
        // wx.clearStorage();
        var that = this;
        that.getRegData(function(){
            app.getStudentInfo(1, function(){
                that.setData({
                    openId: app.globalData.openId,
                    hasReg: app.globalData.hasReg,
                    studentInfo: app.globalData.studentInfo,
                });
            });            
        });
        // that.upodateStudentInfo();

        if (app.globalData.userInfo) {
            this.setData({
                userInfo: app.globalData.userInfo,
                hasUserInfo: true
            })
        } else if (this.data.canIUse) {
            // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
            // 所以此处加入 callback 以防止这种情况
            app.userInfoReadyCallback = res => {
                this.setData({
                    userInfo: res.userInfo,
                    hasUserInfo: true
                })
            }
        } else {
            // 在没有 open-type=getUserInfo 版本的兼容处理
            wx.getUserInfo({
                success: res => {
                    app.globalData.userInfo = res.userInfo
                    this.setData({
                        userInfo: res.userInfo,
                        hasUserInfo: true
                    })
                }
            })
        }

        // 获取错题数
        app.getUseropenId(function(openId) {
            that.data.openId = openId;
            if (openId) {
                app.showLoading();
                wx.request({
                    url: app.api.findQuestionsNumber + openId + '?time=' + Date.parse(new Date()) ,
                    data: {},
                    header: { 'Content-Type': 'application/json' },
                    method: 'POST',
                    success: function(rs) {
                        app.hideLoading();
                        // if(!rs || !rs.data || !rs.data.data) {
                        //     console.log('获取错题数失败：findQuestionsNumbe');
                        //     return;
                        // }
                        var list = rs.data.data;
                        that.setData({
                            questionsNumber: list['questionsNumber'],
                            pushquestionsNumber: list['pushquestionsNumber'],
                        });
                        // console.log(list['questionsNumber'])
                    },
                    fail:function(rs) {
                        app.hideLoading();
                        console.log('获取错题数失败：findQuestionsNumbe')
                    },
                });
            } else {
                console.log('获取openId失败');
            }
        });
    },
    hidePopup(){
        app.globalData.showPopup = 0;
        this.setData({
            showPopup: 0,
        })
    },
    getUserInfo: function(e) {
        console.log(e)
        app.globalData.userInfo = e.detail.userInfo
        this.setData({
            userInfo: e.detail.userInfo,
            hasUserInfo: true
        })
    },
    // upodateStudentInfo:function() {
    //     var that = this;
    //     if (app.globalData.studentInfo) {
    //         that.setData({
    //             studentInfo: app.globalData.studentInfo,
    //             hasReg: app.globalData.hasReg,
    //             openId: app.globalData.openId,
    //         });
    //         // console.log(that.data.hasReg);
    //         // console.log(that.data.studentInfo);
    //         // console.log(that.data.schoolRange);
    //     } else {
    //         setTimeout(function(){
    //             if (app.globalData.studentInfo) {
    //                 that.setData({
    //                     studentInfo: app.globalData.studentInfo,
    //                     hasReg: app.globalData.hasReg,
    //                     openId: app.globalData.openId,
    //                 });
    //             }
    //             // console.log(that.data.hasReg);
    //             // console.log(that.data.studentInfo);
    //             // console.log(that.data.schoolRange);
    //         },500);
    //     }
    // },
    getRegData: function(callback){  
        var that = this;      
        // 获取学校班级年级数据

        //班级 
        var arr_class = ['请选择班级'];
        for (var i = 1; i <= 30; i++) {
            arr_class.push('('+i + ') 班');
        }
        this.setData({
            classRange: arr_class
        });
        // 学校
        app.showLoading();
        wx.request({
            url: app.api.listSchool + '?time=' + Date.parse(new Date()),
            data: {},
            header: { 'Content-Type': 'application/json' },
            method: 'POST',
            success: function(rs) {
                var list = rs.data.data;
                var arr = ['请选择学校'];
                for (var i in list)
                    arr.push(list[i].name);
                // that.globalData.schoolRange = arr;
                that.setData({
                    schoolRange: arr
                });
                // wx.showModal({
                //     title: '打印',
                //     content: 'schoolRange'+that.data.schoolRange,
                // })
                // console.log(that.data.schoolRange)

                //年级
                wx.request({
                    url: app.api.listGrade + '?time=' + Date.parse(new Date()),
                    data: {},
                    header: { 'Content-Type': 'application/json' },
                    method: 'POST',
                    success: function(rs) {
                        app.hideLoading();
                        var list = rs.data.data;
                        var arr = ['请选择年级'];
                        for (var i in list)
                            arr.push(list[i].name);
                        // that.globalData.gradeRange = arr;
                        that.setData({
                            gradeRange: arr
                        });

                        callback && callback();
                    }
                });
            },
            fail:function(error){
                app.hideLoading();
                // wx.showModal({
                //     title: '失败',
                //     content: 'error:'+error,
                // })  
            }
        });
    },
    can_jump: function(e) {
        if(this.data.studentInfo)
            wx.navigateTo({
              url: e.currentTarget.dataset.url
            })
        else
            wx.showModal({
                title: '未注册',
                content: '您还未注册，请先注册！',
                success: function(res) {
                    if (res.confirm) {
                        wx.navigateTo({
                          url: app.globalData.reg_url
                        })
                      // console.log('用户点击确定')
                    } else if (res.cancel) {
                      // console.log('用户点击取消')
                    }
                    
                }
            })
    },
})